package com.itheima.controller;

import com.itheima.anno.LogOperation;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {
    private static final Logger log = LoggerFactory.getLogger(DeptController.class);
    //获取业务层对象
    @Autowired//从 ioc容器中找这个对象
    private DeptService deptService;
    /*
    查询
     */

    @GetMapping(name="查询部门列表")
    public Result selectDept(/*HttpSession session*/){
        /*log.info("查询部门列表");
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser==null){
            return Result.error("请先登录");
        }*/


        List<Dept> depts = deptService.selectDept();
        /*
        返回的是一个Result对象
        当前类上的 @RestController 注解,其中包含了@ResponseBody
        @ResponseBody  : 作用将返回的对象或集合自动转为json
         */
        return Result.success(depts);
    }
    /*
    删除
     */
    @DeleteMapping
    @LogOperation
    public Result deleteDept(Integer id){
        deptService.deleteDept(id);
        return Result.success();
    }

    /*
    新增
     */
    @PostMapping
    @LogOperation
    public Result insertDept(@RequestBody Dept dept){

        deptService.insertDept(dept);

        return Result.success();
    }

    /**
     * 根据id修改部门
     * 数据回显
     */
    @GetMapping("/{id}")
    public Result selectDeptById(@PathVariable Integer id){
        Dept dept = deptService.selectDeptById(id);
        return Result.success(dept);
    }
    /**
     * 修改
     */
    @PutMapping
    @LogOperation
    public Result updateDept(@RequestBody Dept dept){
        deptService.updateDept(dept);
        return Result.success();
    }
}
